package cn.stronglink.assetmanage.business.inventory.model;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.dao.AssetDao;
import cn.stronglink.assetmanage.dao.BrandDao;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.DepartmentDao;
import cn.stronglink.assetmanage.dao.InventoryDao;
import cn.stronglink.assetmanage.dao.InventoryDetailDao;
import cn.stronglink.assetmanage.dao.RoomDao;
import cn.stronglink.assetmanage.dao.TypeDao;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.entity.Brand;
import cn.stronglink.assetmanage.entity.Cabinet;
import cn.stronglink.assetmanage.entity.DataCenter;
import cn.stronglink.assetmanage.entity.Department;
import cn.stronglink.assetmanage.entity.Inventory;
import cn.stronglink.assetmanage.entity.InventoryDetail;
import cn.stronglink.assetmanage.entity.Model;
import cn.stronglink.assetmanage.entity.Room;
import cn.stronglink.assetmanage.entity.Type;
import cn.stronglink.netrequest.business.inventory.InventoryService;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by chencongcong on 2018/6/15.
 */

public class InventoryDetailModel {
    private InventoryService inventoryService;
    private InventoryDetail syncInventoryDetail;
    private onInventoryDetailListener inventoryDetailListener;

    public onInventoryDetailListener getInventoryDetailListener() {
        return inventoryDetailListener;
    }

    public void setInventoryDetailListener(onInventoryDetailListener inventoryDetailListener) {
        this.inventoryDetailListener = inventoryDetailListener;
    }

    public InventoryDetailModel() {
        inventoryService = new InventoryService();
    }

    /**
     * 根据盘点资产id获得盘点详细信息
     *
     * @param asset_id
     * @return
     */
    public InventoryDetail getInventoryDetailById(String asset_id) {
        InventoryDetail inventoryDetail = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().queryBuilder().
                where(InventoryDetailDao.Properties.Id_.eq(asset_id)).list().get(0);
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (inventoryDetail != null) {
            return inventoryDetail;
        }
        return null;
    }

    public InventoryDetail getInventoryDetailByRfid(String rfid) {
        InventoryDetail inventoryDetail = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().queryBuilder().
                where(InventoryDetailDao.Properties.RfidNo.eq(rfid)).unique();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (inventoryDetail != null) {
            return inventoryDetail;
        }
        return null;
    }

    public InventoryDetail getInventoryDetailByOrderAndRfid(String orderId, String rfid) {
        try {
            InventoryDetail inventoryDetail = DaoManager.
                    getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().queryBuilder().
                    where(InventoryDetailDao.Properties.OrderNo.eq(orderId),
                            InventoryDetailDao.Properties.RfidNo.eq(rfid)).unique();
            DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
            if (inventoryDetail != null) {
                return inventoryDetail;
            }
        } catch (Exception e) {
            return null;
        }
        return null;
    }

    private Inventory getInventoryByI(String inventory_id) {
        Inventory inventory = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDao().queryBuilder().
                where(InventoryDao.Properties.Id_.eq(inventory_id)).list().get(0);
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (inventory != null) {
            return inventory;
        }
        return null;
    }

    public long getRoomIndex(String roomName) {
        Room room = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getRoomDao().
                queryBuilder().where(RoomDao.Properties.Name.eq(roomName)).list().get(0);
        return room.getRoom_id();
    }

    public String getRoomId_(String roomName) {
        Room room = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getRoomDao().
                queryBuilder().where(RoomDao.Properties.Name.eq(roomName)).list().get(0);
        return room.getId_();
    }

    /**
     * 获取地点列表
     *
     * @return
     */
    public List<Room> getRoomData() {
        List<Room> roomList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getRoomDao().loadAll();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();

        if (roomList == null) {
            roomList = new ArrayList<>();
        }
        Room model = new Room();
        model.setId_("");
        model.setName("请选择地点");
        roomList.add(0, model);
        return roomList;
    }

    public long getBrandIndex(String brandName) {
        Brand brand = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getBrandDao().
                queryBuilder().where(BrandDao.Properties.Name.eq(brandName)).list().get(0);
        return brand.getBrand_id();
    }

    /**
     * 获取品牌列表
     *
     * @return
     */
    public List<Brand> getBrandData() {
        List<Brand> brandList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getBrandDao().loadAll();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (brandList == null) {
            brandList = new ArrayList<>();
        }
        Brand model = new Brand();
        model.setId_("");
        model.setName("请选择品牌");
        brandList.add(0, model);
        return brandList;
    }

    public long getTypeIndex(String typeName) {
        Type type = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getTypeDao().
                queryBuilder().where(TypeDao.Properties.Name.eq(typeName)).list().get(0);
        return type.getType_id();
    }

    /**
     * 获取类型列表
     *
     * @return
     */
    public List<Type> getTypeData() {
        List<Type> typeList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getTypeDao().loadAll();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (typeList == null) {
            typeList = new ArrayList<>();
        }
        Type model = new Type();
        model.setId_("");
        model.setName("请选择类别");
        typeList.add(0, model);
        return typeList;
    }

    public long getDataCenterIndex(String datacenterId_) {
        DataCenter dataCenter = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getDataCenterDao().
                queryBuilder().where(TypeDao.Properties.Id_.eq(datacenterId_)).list().get(0);
        return dataCenter.getData_center_id();
    }

    /**
     * 获取数据中心列表
     *
     * @return
     */
    public List<DataCenter> getDataCenter() {
        List<DataCenter> dataCenterList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getDataCenterDao().loadAll();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (dataCenterList != null) {
            return dataCenterList;
        }
        return null;
    }

    public long getUseDepart(String useBranchId_) {
        Department department = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getDepartmentDao().
                queryBuilder().where(DepartmentDao.Properties.Id_.eq(useBranchId_)).list().get(0);
        return department.getDept_id();
    }

    /**
     * 获取部门列表
     *
     * @return
     */
    public List<Department> getDepartment() {
        List<Department> departments = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getDepartmentDao().loadAll();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (departments == null) {
            departments = new ArrayList<>();
        }
        Department model = new Department();
        model.setId_("");
        model.setName("请选择部门");
        model.setPid_("");
        departments.add(0, model);
        return departments;
    }

    /**
     * 获取机柜列表
     *
     * @return
     */
    public List<Cabinet> getCabinet() {
        List<Cabinet> cabinetList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getCabinetDao().loadAll();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (cabinetList == null) {
            cabinetList = new ArrayList<>();
        }
        Cabinet cabinet = new Cabinet();
        cabinet.setId_("");
        cabinet.setAssetName("请选择机柜");
        cabinet.setRoomId(0L);
        cabinetList.add(0, cabinet);
        return cabinetList;
    }


    public void saveInventoryData(InventoryDetail saveInventoryDetail) {
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().insertOrReplace(saveInventoryDetail);
    }

    public void syncInventoryData(InventoryDetail inventoryDetail) {
        syncInventoryDetail = inventoryDetail;
        //访问服务器，同步数据
        Inventory inventory = getInventoryByI(inventoryDetail.getOrderNo());
        inventory.setId(Long.valueOf(inventory.getId_()));
        inventoryDetail.setId(Long.valueOf(inventoryDetail.getId_()));
        final List<InventoryDetail> inventoryDetailList = new ArrayList<>();
        inventoryDetailList.add(inventoryDetail);
        Gson gson = new Gson();
        String inventoryDetailJson = gson.toJson(inventoryDetailList);
        try {
            inventoryService.uploadInventoryList(inventoryDetailJson, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    if (inventoryDetailListener != null) {
                        inventoryDetailListener.onFailure(call, e);
                    }
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (inventoryDetailListener != null) {
                        inventoryDetailListener.onResponse(call, response);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void deleteSyncData() {
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().delete(syncInventoryDetail);
    }

    public Inventory getInventoryById(String orderId) {
        Inventory inventory = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDao().queryBuilder().
                where(InventoryDao.Properties.Id_.eq(orderId)).unique();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (inventory != null) {
            return inventory;
        }
        return null;
    }

    public interface onInventoryDetailListener {
        void onFailure(Call call, IOException e);

        void onResponse(Call call, Response response);
    }

    public List<Model> getModelList() {
        List<Model> modelList = new ArrayList<>();
        Model model = new Model();
        model.setBrandId_("");
        model.setTypeId_("");
        model.setId_("");
        model.setName("请选择型号");
        modelList.add(model);
        List<Model> models = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getModelDao().loadAll();
        if (models != null && models.size() > 0) {
            modelList.addAll(models);
        }
        return modelList;
    }

    public String getManageName(String id) {
        String name = "";
        try {
            Department department = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getDepartmentDao()
                    .queryBuilder().where(DepartmentDao.Properties.Id_.eq(id)).unique();
            if (department != null) {
                name = department.getName();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return name;
    }
}
